<template>
  <div class="customer-service">
    <!-- 悬浮咨询按钮 -->
    <div class="consult-button" @click="showChat = true">
      <div class="icon-wrapper">
        <img src="https://cdn.iconscout.com/icon/free/png-256/free-customer-service-1851259-1569168.png" alt="客服" class="service-icon">
      </div>
      <span class="text">立即咨询</span>
    </div>

    <!-- 聊天对话框 -->
    <el-dialog
      v-model="showChat"
      title="在线客服"
      width="360px"
      :close-on-click-modal="false"
      :show-close="true"
      custom-class="chat-dialog"
    >
      <div class="chat-container">
        <!-- 客服信息头部 -->
        <div class="service-header">
          <img src="https://cdn.iconscout.com/icon/free/png-256/free-customer-service-1851259-1569168.png" alt="客服头像" class="avatar">
          <div class="info">
            <div class="name">在线客服</div>
            <div class="status">在线</div>
          </div>
        </div>

        <!-- 聊天记录区域 -->
        <div class="chat-messages" ref="messagesContainer">
          <div class="message service">
            <div class="message-content">
              您好，请问有什么可以帮助您的吗？
            </div>
            <div class="time">{{ getCurrentTime() }}</div>
          </div>
          <template v-for="(msg, index) in messages" :key="index">
            <div class="message" :class="msg.type">
              <div class="message-content">{{ msg.content }}</div>
              <div class="time">{{ msg.time }}</div>
            </div>
          </template>
        </div>

        <!-- 输入区域 -->
        <div class="chat-input">
          <el-input
            v-model="inputMessage"
            type="textarea"
            :rows="3"
            placeholder="请输入您要咨询的内容..."
            @keyup.enter.native="sendMessage"
          />
          <el-button type="primary" @click="sendMessage" :disabled="!inputMessage.trim()">
            发送
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'CustomerService',
  setup() {
    const showChat = ref(false)
    const inputMessage = ref('')
    const messages = ref([])
    const messagesContainer = ref(null)

    const getCurrentTime = () => {
      const now = new Date()
      return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`
    }

    const sendMessage = () => {
      if (!inputMessage.value.trim()) return

      // 添加用户消息
      messages.value.push({
        type: 'user',
        content: inputMessage.value,
        time: getCurrentTime()
      })

      // 模拟客服回复
      setTimeout(() => {
        messages.value.push({
          type: 'service',
          content: '感谢您的咨询，我们的客服会尽快回复您。',
          time: getCurrentTime()
        })
      }, 1000)

      inputMessage.value = ''
      
      // 滚动到底部
      setTimeout(() => {
        if (messagesContainer.value) {
          messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
        }
      }, 100)
    }

    return {
      showChat,
      inputMessage,
      messages,
      messagesContainer,
      getCurrentTime,
      sendMessage
    }
  }
}
</script>

<style scoped>
.customer-service {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 2000;
}

.consult-button {
  display: flex;
  align-items: center;
  background: #ff6b6b;
  padding: 8px 16px;
  border-radius: 24px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
}

.consult-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.icon-wrapper {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}

.service-icon {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.text {
  color: white;
  font-size: 14px;
  font-weight: 500;
}

.chat-dialog {
  border-radius: 8px;
}

.chat-container {
  display: flex;
  flex-direction: column;
  height: 500px;
}

.service-header {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #eee;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
}

.info .name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.info .status {
  font-size: 12px;
  color: #67c23a;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: #f7f7f7;
}

.message {
  margin-bottom: 16px;
}

.message.service .message-content {
  background: white;
  margin-right: 48px;
}

.message.user {
  text-align: right;
}

.message.user .message-content {
  background: #95ec69;
  margin-left: 48px;
}

.message-content {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 4px;
  max-width: 80%;
  word-break: break-word;
}

.time {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.chat-input {
  padding: 16px;
  border-top: 1px solid #eee;
  background: white;
}

.chat-input .el-button {
  margin-top: 8px;
  width: 100%;
}

:deep(.el-dialog__body) {
  padding: 0;
}

:deep(.el-dialog__header) {
  display: none;
}
</style> 